package com.fdb.efp.limit.service.daybat;

import java.util.HashMap;
import java.util.Map;

import com.fdb.efp.limit.service.dao.LmtPrdContBakDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.constant.BaseConstant;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.console.service.facade.PubSysInfoService;
import com.fdb.efp.limit.service.facade.BatLmtPrdContBakService;

/**
 * <p>Description:授信协议表				<p>
 * @author chenqm1
 * @date 2018-05-09
 */

@Service("batLmtPrdContBakService")
public class BatLmtPrdContBakServiceImpl implements BatLmtPrdContBakService, FrameworkService{
	private static Logger logger = LoggerFactory.getLogger(BatLmtPrdContBakServiceImpl.class);
	
	@Autowired
	private LmtPrdContBakDao lmtPrdContBakDao;
	
	@Autowired
	private PubSysInfoService pubSysInfoService;
	
	/**
	 * 本地服务器地址
	 */
	@Override
	public void batchInsertLmtPrdContBak() throws Exception{
	    logger.info("日终批量任务：备份授信协议开始......");
		try {
		    String openDay = pubSysInfoService.getLastOpenday(BaseConstant.SYS_ID_1001);
            logger.info("系统营业日期：" + openDay);
            
            String befDate = TimeUtil.addDay(openDay, -7);
            
            logger.info("授信协议开始删除备份日期为【"+befDate+"】的数据......");
            Map<String, Object> params = new HashMap<>();
            params.put("bakDate", befDate);
            params.put("openday", openDay);
            lmtPrdContBakDao.deleteByBakDate(params);
            
            //支持重复执行，先删除当天备份的数据
            logger.info("授信协议开始删除备份日期为【"+openDay+"】的数据......");
            params.put("bakDate", openDay);
            lmtPrdContBakDao.deleteByBakDate(params);
            
            logger.info("授信协议开始备份数据，备份日期【"+openDay+"】......");
            lmtPrdContBakDao.batchInsertLmtPrdContBak(params);
            
            logger.info("日终批量任务：备份授信协议结束......");
		} catch (Exception e) {
			logger.error("授信协议数据备份失败！",e);
			throw new Exception("授信协议数据备份失败！",e);
		}
	
	}

}
	
